<#import "../tpl/pageTep.ftl" as page>
<@page.pageBase currentMenu="礼券管理">
<style>
    .state{
        cursor: pointer;
    }
</style>
    <!--suppress ALL -->
<form id="searchForm" class="form-panel" action="${basepath}/rest/manage/voucher/selectVoucherPage">
    <ul class="panel-content">
        <li>
            <div>
                <div class="control-group span5">
                    <div class="search-controls  controls">
                        <input type="text" name="name" id="name" value="" placeholder="搜索礼券名称、礼券类型">
                    </div>
                </div>
                <div class="form-actions span2">
                    <button type="submit" class="button  button-primary">搜索</button>
                </div>
            </div>
            <div class="form-actions">
                <a href="${basepath}/rest/manage/voucher/toAdd" class="button button-success">
                    添加
                </a>
                <a class="button button-danger" onclick="return deletesFunction();">
                     批量删除
                </a>
            </div>
        </li>
    </ul>
</form>
    <div id="grid"></div>
<script>
    var Grid = BUI.Grid,
            Store = BUI.Data.Store,
            columns = [
                {title: '序号', dataIndex: 'id', elCls: 'center', width: '20%'},
                {
                    title: '礼券类型', dataIndex: 'type', elCls: 'center', width: '20%',
                    renderer: function (value) {
                        switch (value) {
                            case 'cash':
                                return '代金券';
                                break;
                            case 'discount':
                                return '打折券';
                                break;
                            case 'birthday':
                                return '生日券';
                                break;
                            default:return value;
                        }
                    }
                },
                {title: '礼券名称', dataIndex: 'name', elCls: 'center', width: '20%'},
                {title: '兑换积分', dataIndex: 'score', elCls: 'center', width: '20%'},
                {title: '库存', dataIndex: 'stock', elCls: 'center', width: '20%'},
                {
                    title: '指派记录',
                    dataIndex: 'sendAmount',
                    elCls: 'center',
                    width: '20%',
                    renderer: function (value, data) {
                        value=value == '0'?'无':'查看';
                        return '<a  href="/rest/manage/voucher/toVoucherSend?id=' + data.id + '">'+value+'</a>';
                    }
                },
                {
                    title: '礼券状态', dataIndex: 'status', elCls: 'center', width: '20%', renderer: function (value,data) {
                        var state=value=='y'?'启用':'禁用';
                        return  '<a class="state" data-state="'+value+'" data-id="'+data.id+'">'+state+'</a>';
                    }
                },
                {
                    title: '操作', dataIndex: 'id', elCls: 'center', width: '20%',
                    renderer: function (value, obj, index) {
                        return '<a  href="${basepath}/rest/manage/voucher/toEdit?id=' + value + '">编辑</a>' +
                                '<a style="margin-left:10px;"  href="javascript: deleteFunction(' + value + ');">删除</a>';
                    }
                }
            ];

    var store = new Store({
                url: '/rest/manage/voucher/selectVoucherPage',
                autoLoad: true, //自动加载数据
                params: { //配置初始请求的参数
                    length: '10',
                    status: $("#status").val()
                },
                pageSize: 10,	// 配置分页数目
                root: 'list',
                totalProperty: 'total'
            }),
            grid = new Grid.Grid({
                render: '#grid',
                columns: columns,
                loadMask: true, //加载数据时显示屏蔽层
                store: store,
                plugins: [Grid.Plugins.CheckSelection], // 插件形式引入多选表格
                // 底部工具栏
                bbar: {
                    pagingBar: true
                }
            });

    grid.render();
    //修改状态
    function changeStatus(status,id){
       $.ajax({
           url:'/rest/manage/voucher/updateJson',
           type:'POST',
           dataType:'json',
           data:{
               status:status,
               id:id
           },
           success:function () {
               var obj=new Object();
               obj.start=0;
               store.load(obj);
           }
       })
    }
    $(document).on('click','.state',function () {
      var state=$(this).attr('data-state');
      var id=$(this).attr('data-id');
        state=state=='y'?'n':'y';
        changeStatus(state,id)
    });
    //单个删除操作
    function deleteFunction(value) {
        BUI.Message.Confirm('确认要删除吗？', function () {
            $.ajax({
                type: "POST",
                url: "${basepath}/rest/manage/voucher/deleteJson",
                dataType: "json",
                data: {
                    id: value
                },
                success: function (data) {
                    var obj = new Object();
                    obj.start = 0; //返回第一页
                    store.load(obj);
                }
            });
        }, 'question');
    }


    //删除选中的记录--批量删除
    function deletesFunction() {
        BUI.Message.Confirm('确认要删除吗？', function () {
            var selections = grid.getSelection();
            var ids = new Array();
            for (var i = 0; i < selections.length; i++) {
                ids[i] = selections[i].id.toString()
            }
            $.ajax({
                type: "POST",
                url: "${basepath}/rest/manage/voucher/deletesJson",
                dataType: "json",
                data: {
                    ids: ids
                },
                success: function (data) {
                    var obj = new Object();
                    obj.start = 0; //返回第一页
                    store.load(obj);
                }
            });
        }, 'question');
    }

    //模糊查询
    var form = new BUI.Form.HForm({
        srcNode: '#searchForm'
    }).render();

    form.on('beforesubmit', function (ev) {
        //序列化成对象
        var obj = form.serializeToObject();
        obj.start = 0; //返回第一页
        store.load(obj);
        return false;
    });
</script>

</@page.pageBase>
